<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



include "/library/product_return.php";


if(isset($_GET['searchID'])){
    query_product($_GET['searchID']);
    exit();
}
if(isset($_GET['return'])&&isset($_GET['returndata'])){
    return_product($_GET);
exit();
    //header('location: customer_type.php');
}
if(isset($_GET['ratio'])&&isset($_GET['id'])&&isset($_GET['sort'])&&isset($_GET['addStock'])){
    addStock($_GET);

    //header('location: customer_type.php');
}
if(isset($_GET['edit'])){
    editProduct($_GET);
    //header('location: product.php');
}
include "header.php";
?>
<head>
<script type="text/javascript" src="js/product_add.js"></script>
<script type="text/javascript">
var ratioCur=0;
var buyCur=0;
    function selectTable(input,ratio,buyprice){
        $(document).ready(function(){

            $('[name="return"] ').eq(input).attr('checked','checked');
            ratioCur=ratio;
            buyCur=buyprice;
            $('#money').html($('[name="returndata"]').val()*ratioCur*buyCur);
        });
    }
    $(document).ready(function(){
        $('#Select').hide();
        $('#tableReceipt').hide();
        
        $('#receipt_id :button').click(function(){
            var globaldata = null;
            $.getJSON("product_return_add.php",{'searchID':"'"+$('#receipt_id :text').val()+"'"}, function(data){
               globaldata=data;
                var tblRow = "";
                $.each(data.type, function(i,result){
			tblRow = tblRow + "<tr onclick='selectTable("+i+","+result.ratio+","+result.buyprice+");'><td><input type='radio' name='return' onClick='alert();' value='"+result.id+"' ></td>";
                        tblRow = tblRow + "<td>"+result.product_id+"</td>";
                        tblRow = tblRow + "<td>"+result.name+"</td>";
                        tblRow = tblRow + "<td>"+result.Typename+"</td>";
                        tblRow = tblRow + "<td>"+result.ratio+"</td>";
                        tblRow = tblRow + "<td>"+result.amount+"</td>";
                        tblRow = tblRow + "<td>"+result.buyprice+"</td>";
                         tblRow = tblRow + "</tr>";
                              //          addProduct(result.id,result.product_type_id,0);
                                        
		});
                $('#tableReceipt').show();
                $("#dataflow").html(tblRow);
                $('#Select').show();
                $("#Select").html(tblRow);
            });
            
            
        });
        $("#Select").change(function(){
                //$('#unitname').html('dsa123d');
                $.each(globaldata.type, function(i,result){
                    if($('#Select').val() == result.id){
                        $('#unitname').html(result.nameunit);
                        $('#ratio').html(result.ratio+'<input type="hidden" name="ratio" value="'+result.ratio+'">');
                        $('#buyprice').html(result.buyprice);
                    }
		});
            });
        $('[name="returndata"]').change(function(){
                
                $('#money').html($(this).val()*ratioCur*buyCur);
            });
    });


</script>
</head>
<div id="product" align="center" width="100%">
    <form action="product_return_add.php" method="get" id="form">
        <?php if(isset($_GET['editform'])){
            while($transaction = mysql_fetch_array($printP)){
            ?>
            <script type="text/javascript">
                addProduct('<?=$transaction['product_id']; ?>','<?=$transaction['product_type_id']; ?>','<?=$transaction['amount']; ?>');

            </script>

       <? }
       } ?>
    <table  id="tableline" >
        <tr>
            <th>เลขที่ใบเสร็จ</th>
            <td id="receipt_id" ><input type="text"  name="receipt_id" value=""><input type="button" value="ค้นหา"></td>
        </tr>
    </table>

                <table id="tableReceipt">
                    <tr>
                         <th>เลือก</th>
                        <th>รหัสสินค้า</th>
                        <th>ชื่อสินค้า</th>
                        <th>ประเภทการขาย</th>
                        <th>อัตราต่อหนวย</th>
                        <th>จำนวนที่ซื้อมา</th>
                        <th>ราคาขายต่อหน่วย</th>

                    </tr>
                    <tbody id="dataflow">
                    </tbody>
                </table>

      <table  id="tableline" >
        <tr>
            <th>จำนวนคืน</th>
            <td id="return" ><input type="text"  name="returndata" value=""></td>
        </tr>
        <tr>
            <th>จำนวนเงิน</th>
            <td id="money" ></td>
        </tr>




    </table>
     <div >
        <input type="submit" id="add" name="add" value=" คืนสินค้า ">
</div>
    </form>

    <p>
    </p>

</div>
